package com.example.distillapplication.ui.fragment.login;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.distillapplication.R;
import com.example.distillapplication.data.entity.OperationLog;
import com.example.distillapplication.data.entity.UserInfo;
import com.example.distillapplication.data.repository.DataBaseRepository;
import com.example.distillapplication.databinding.FragmentLoginBinding;
import com.example.distillapplication.ui.fragment.base.BaseFragment;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class LoginFragment extends BaseFragment {
    FragmentLoginBinding fragmentLoginBinding;
    DataBaseRepository dataBaseRepository;
    LoginListener loginListener;
    String userName;
    String passWord;

    public LoginFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        loginListener = (LoginListener) mActivity;
        View view = inflater.inflate(R.layout.fragment_login, container, false);
        fragmentLoginBinding = DataBindingUtil.bind(view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        dataBaseRepository = new DataBaseRepository(getContext(), mDataBaseViewModel);


        fragmentLoginBinding.login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userName = fragmentLoginBinding.userName.getText().toString();
                passWord = fragmentLoginBinding.password.getText().toString();
                if(userName.equals("") || passWord.equals("")){
                    //loginListener.login();
                    showLongToast("用户名或密码不能为空！");
                }else{
                    List<UserInfo> userInfos = new ArrayList<>();
                    userInfos = mDataBaseViewModel.dataBaseRepository.getUserInfoByAccount(userName);
                    if(userInfos.size() == 0){
                        showLongToast("不存在此用户！");
                    }else if(!userInfos.get(0).getUserPassword().equals(passWord)){
                        showLongToast("密码错误！");
                    }else if(!mInstrumentStateViewModel.instrumentSelfTest.getValue()){
                        showLongToast("开机自检未通过！请检查设备！");
                    } else {
                        mSharedViewModel.userinfo.postValue(userInfos.get(0));
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

                        mSharedViewModel.operationLog.postValue(new OperationLog(userInfos.get(0).getUserId(),
                                df.format(new Date()), "", ""));
                        loginListener.login();
                    }
                }
            }
        });


    }
}
